from .models import *
from datetime import datetime


def HLSer(query):
    data = []
    for i in query:
        js = {}
        js['update'] = i.update.split(' ')[0]
        js['upd'] = i.update
        js['json'] = eval(i.json)
        data.append(js)

    data = sorted(data, key=lambda x: datetime.strptime(x['upd'], '%Y-%m-%d %H:%M:%S'), reverse=False)
    for i in data:
        del i['upd']

    return data


def HLSocketSer(query):
    data = []
    for i in query:
        js = {}
        js['date'] = i.date
        js['time'] = i.time
        js['json'] = eval(i.json)
        data.append(js)

    return sorted(data, key=lambda x: datetime.strptime(x['time'], '%H:%M:%S'), reverse=False)


def OrderSer(query):
    data = []
    for i in query:
        for j in i:
            js = {}
            js['order_id'] = j.order_id
            js['money'] = j.money
            js['create'] = j.create
            data.append(js)

    return sorted(data, key=lambda x: x['create'], reverse=True)

def OrderItemSer(query):
    data = []
    for i in query:
        for j in i:
            js = {}
            js['order_id'] = j.order_id
            js['money'] = j.money
            js['create'] = j.create
            data.append(js)

    return sorted(data, key=lambda x: x['create'], reverse=True)